<?php
class FormHtml
{
	private $o_form;
	private $arr_o_fields;
	private $arr_o_fieldSets;
	private $arr_fieldsHtml;
	private $arr_fieldSetsHtml;
	private $s_formLineElem;
	
	public function __construct($o_form, $arr_o_fieldSets, $arr_o_fields, $s_formLineElem)
	{
		$this->o_form = $o_form;
		$this->arr_fieldSetsHtml = array();
		$this->arr_fieldsHtml = array();
		$this->s_formLineElem = $s_formLineElem;
		$this->arr_o_fieldSets = $arr_o_fieldSets;
		$this->arr_o_fields = $arr_o_fields;
		
		$this->makeFieldSetsHtml();
	}
	
	public function getFormHtml()
	{
		
		return $s_formHtml;
	}
	
	public function getHtml()
	{
		$s_formHtml = '<form action="'.$this->o_form->getAction().'" method="'.$this->o_form->getMethod().'" name="'.$this->o_form->getName().'" id="'.$this->o_form->getId().'">';
		
		foreach($this->arr_fieldSetsHtml as $s_fieldSetId=> $s_fieldSetHtml){
			$s_formHtml .= $s_fieldSetHtml;
		}
		$s_formHtml .= '</form>';
		
		return $s_formHtml;
	}
	
	private function makeFieldSetsHtml()
	{
		foreach($this->arr_o_fieldSets as $s_fieldSetId=> $o_fieldSet){
			$this->arr_fieldSetsHtml[$s_fieldSetId] = $this->getFieldSetHtml($o_fieldSet);
		}
	}
	
	private function getFieldSetHtml($o_fieldSet)
	{
		$s_fieldSetHtml = '<fieldset id="' . $o_fieldSet->getFieldSetId() . '" >';
		$s_fieldSetHtml .= '<legend>' . $o_fieldSet->getFieldSetLegend() . '</legend>';
		
		$arr_fieldSetFields = $o_fieldSet->getFieldSetFields();
		
		//display errors in one paragraph with list items
		if($o_fieldSet->getInlineErrors() == false){
			$s_fieldSetHtml .= $this->getFieldSetErrorBox($arr_fieldSetFields);
		}
		//loop through the fieldset fields then build the html
		foreach($arr_fieldSetFields as $s_fieldId){
			$o_field = $this->arr_o_fields[$s_fieldId];
			$s_fieldSetHtml .= $this->getFormLine($o_field, $o_fieldSet->getInlineErrors());
		}
		$s_fieldSetHtml .= '</fieldset>';
		return $s_fieldSetHtml;
	}
	
	private function getFieldSetErrorBox($arr_fieldSetFields)
	{
		$s_fieldSetErrorBox = '<p class="form-line-error">';
		$s_fieldSetErrorBox .= '<ul>';
		//loop through the fieldset fields to get the error messages
		foreach($arr_fieldSetFields as $s_fieldId){
				$o_field = $this->arr_o_fields[$s_fieldId];
				if($o_field->getErrorMessage() != ''){
					if($o_field->getFieldType() == 'checkbox' || $o_field->getFieldType() == 'radio'){
						$s_label = $o_field->getFieldName();
					}else{
						$s_label = $o_field->getFieldLabel();
					}
					$s_fieldSetErrorBox .= '<li>';
					$s_fieldSetErrorBox .=  $s_label . ': ' . $o_field->getErrorMessage();
					$s_fieldSetErrorBox .= '</li>';
				}
		}
		$s_fieldSetErrorBox .= '</ul>';
		$s_fieldSetErrorBox .= '</p>';
		
		return $s_fieldSetErrorBox;
	}
	
	private function getFormLine($o_field, $b_inlineErrors)
	{
		
		$s_class = '';
		$s_style = '';
		if($o_field->getErrorMessage() != ''){
			$s_class = 'class="with-error"';
		}
		if($o_field->getFieldType() == 'hidden'){
			$s_style = 'style="display:none;"';
		}
		$s_formLine = '<'. $this->s_formLineElem .' ' . $s_class . ' '. $s_style .'>';
		//field label line
		$s_formLine .= $this->getFormLineLabel($o_field);
		//field input itself
		$s_formLine .= $this->getFieldHtml($o_field);
		//field error message line
		if($b_inlineErrors == true){
			$s_formLine .= $this->getFormLineError($o_field);
		}
		$s_formLine .= '</'. $this->s_formLineElem .'>';
		return $s_formLine;
	}
	
	private function getFormLineError($o_field)
	{
		$s_formLineError = '';
		$s_style = '';
		if($o_field->getErrorMessage() == ''){
			//empty error so hide the container
			$s_style = 'style="display:none;"';
		}
		
		$s_formLineError .= '<label for="' . $o_field->getFieldId() . '" class="form-line-error" '.$s_style.'>';
		$s_formLineError .= $o_field->getErrorMessage();
		$s_formLineError .= '</label>';
		return $s_formLineError;
	}
	
	private function getFormLineLabel($o_field)
	{
		$s_formLineLabel = '';
		if($o_field->getFieldLabel() != ''){
			$s_formLineLabel = '<label for="' . $o_field->getFieldId() . '">';
			$s_formLineLabel .= $o_field->getFieldLabel();
			$s_formLineLabel .= '</label>';
		}
		return $s_formLineLabel;
	}
	
	private function getFieldHtml($o_field)
	{
		$s_fieldHtml = '';
		switch($o_field->getFieldType()){
			case 'text':
			case 'password':
				$s_fieldHtml = '<input type="' . $o_field->getFieldType() . '" id="' . $o_field->getFieldId() . '" name="' . $o_field->getFieldName() . '" value="' . $o_field->getFieldValue() . '" maxlength="' . $o_field->getMaxLength() . '"/>';
				break;
	
			case 'radio':
			case 'checkbox':
				$s_checked = '';
				if($o_field->getIsDefault() == true){
					$s_checked = ' checked ';
				}
				$s_fieldHtml = '<input type="' . $o_field->getFieldType() . '" id="' . $o_field->getFieldId() . '" name="' . $o_field->getFieldName() . '" value="' . $o_field->getFieldValue() . '" '. $s_checked .' />';
				break;
			
			case 'textarea':
				$s_fieldHtml = '<textarea id="' . $o_field->getFieldId() . '" name="' . $o_field->getFieldName() . '" rows="4" cols="40">' . $o_field->getFieldValue() . '</textarea>';
				break;
				
			case 'submit':
				$s_fieldHtml = '<input type="' . $o_field->getFieldType() . '" id="' . $o_field->getFieldId() . '" name="' . $o_field->getFieldName() . '" value="' . $o_field->getFieldValue() . '" />';
				break;
	
			default:
				$s_fieldHtml = '<input type="' . $o_field->getFieldType() . '" id="' . $o_field->getFieldId() . '" name="' . $o_field->getFieldName() . '" value="' . $o_field->getFieldValue() . '" maxlength="' . $o_field->getMaxLength() . '"/>';
			break;
		}
	
		return $s_fieldHtml;
	}
}